/** @format */

let oldArrayMethods = Array.prototype;
// value.__proto__ = arrayMethods;
// arrayMethods.__proto__ = oldArrayMethods

export let arrayMethods = Object.create(oldArrayMethods);

const methods = ['push', 'shift', 'unshift', 'pop', 'sort', 'splice', 'reverse'];

methods.forEach((methodName) => {
  arrayMethods[methodName] = function (...args) {
    console.log('用户调用了push方法');
    const result = oldArrayMethods[methodName].call(this, args);
    const ob = this.__ob__;

    let inserted; // 当前用户插入的元素
    switch (methodName) {
      case 'push':
      case 'unshift':
        inserted = args;
        break;
      case 'splice':
        inserted = args.slice(2);
    }

    if (inserted) ob.observerArray(inserted); // 将新增属性继续观测

    return result;
  };
});
